﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using System.Windows.Forms;
using System.IO;
using System.Web.Security;

namespace Design
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.IsInRole("administrator"))
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private void AssignRoles()
        {
            try
            {
                if (radgeneralemp.Checked == false && radmanager.Checked == false)
                {
                    MessageBox.Show(null, "Please Select User Role", "User Role", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    if (radmanager.Checked == true)
                    {
                        Roles.AddUserToRole(CreateUserWizard1.UserName, "manager");
                    }
                    else
                        if (radgeneralemp.Checked == true)
                        {
                            Roles.AddUserToRole(CreateUserWizard1.UserName, "employee");
                        }
                
            }
            catch (Exception ex)
            {
                
            }
        }

        protected void btnDeleteEquip_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateEquip equip_del = new UpdateEquip();
                equip_del.Equip_ID = txtEquipID.Text;
                equip_del.delete_equipment();

                txtEquipID.Text = "";
                txtEquipName.Text = "";
                txtEquipDesc.Text = "";
                txtEquipPrice.Text = "";
                imgDisplay.ImageUrl = "";
            }
            catch
            {
                MessageBox.Show("Enter the Equipment ID to delete", "Delete Equipment", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        protected void btnSearchEquip_Click(object sender, EventArgs e)
        {
            UpdateEquip equip = new UpdateEquip();
            List<UpdateEquip> objEquip = new List<UpdateEquip>();
            objEquip = equip.get_equipment_even_by_id();

            int count = objEquip.Count();
            for (int i = 0; i < count; i++)
            {
                if (txtEquipID.Text == objEquip[i].Equip_ID)
                {
                    try
                    {
                        txtEquipID.Text = objEquip[i].Equip_ID;
                        txtEquipName.Text = objEquip[i].Name;
                        txtEquipPrice.Text = Convert.ToString(objEquip[i].Price);
                        txtEquipDesc.Text = objEquip[i].Description;
                        imgDisplay.ImageUrl = objEquip[i].image;
                        drpCategory.SelectedValue = objEquip[i].Category;
                    }
                    catch
                    {
                        MessageBox.Show("Equipment does not exist!", "Search Equipment", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        protected void btnAddEquip_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("EquipmentImages/");
                if (fluUploadImage.HasFile)
                {
                    string ext = Path.GetExtension(fluUploadImage.FileName);
                    if (ext == ".jpg" || ext == ".png")
                    {
                        fluUploadImage.SaveAs(path + fluUploadImage.FileName);
                        string imagename = "~/EquipmentImages/" + fluUploadImage.FileName;

                        AddEquip Equip = new AddEquip();

                        Equip.Equip_ID = txtEquipID.Text;
                        Equip.Name = txtEquipName.Text;
                        Equip.Price = Convert.ToDecimal(txtEquipPrice.Text);
                        Equip.Description = txtEquipDesc.Text;
                        Equip.Category = drpCategory.Text;
                        Equip.image = imagename;

                        Equip.save_equipment();

                        MessageBox.Show("Equipment Added", "Add Equipment", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    else
                    {

                    }
                }
                else
                {

                }
            }
            catch
            {

            }
        }

        protected void btnUpdateEquip_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("EquipmentImages/");
                if (fluUploadImage.HasFile)
                {
                    string ext = Path.GetExtension(fluUploadImage.FileName);
                    if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
                    {
                        fluUploadImage.SaveAs(path + fluUploadImage.FileName);
                        string imagename = "~/EquipmentImages/" + fluUploadImage.FileName;

                        UpdateEquip objUpdateEquip = new UpdateEquip();

                        objUpdateEquip.Equip_ID = txtEquipID.Text;
                        objUpdateEquip.Name = txtEquipName.Text;
                        objUpdateEquip.Price = Convert.ToDecimal(txtEquipPrice.Text);
                        objUpdateEquip.Description = txtEquipDesc.Text;
                        objUpdateEquip.image = imagename;
                        objUpdateEquip.Category = drpCategory.SelectedValue;

                        objUpdateEquip.update_equipment();

                        MessageBox.Show("Equipment Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Only .JPG, .JPEG and .YPNG Files Allowed!", "Extension Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("Please Select A Picture!", "Image Selection", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch
            {
                MessageBox.Show("Enter Infomration To Update", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            AssignRoles();

            SignUp objadduser = new SignUp();

            objadduser.username = CreateUserWizard1.UserName.ToString();
            objadduser.password = CreateUserWizard1.Password.ToString();
            objadduser.email = CreateUserWizard1.Email.ToString();
            objadduser.type = "administrator";
            objadduser.signup_date = DateTime.Now;

            objadduser.add_user();
        }


        public void btnAddCat(object sender, EventArgs e)
        {

            //try
            //{
            //    string path = Server.MapPath("EquipmentImages/");
            //    if (fluUploadImage.HasFile)
            //    {
            //        string ext = Path.GetExtension(fluUploadImage.FileName);
            //        if (ext == ".jpg" || ext == ".png")
            //        {
            //            fluUploadImage.SaveAs(path + fluUploadImage.FileName);
            //            string imagename = "~/EquipmentImages/" + fluUploadImage.FileName;

            //            AddEquip Equip = new AddEquip();

            //            Equip.Equip_ID = txtEquipID.Text;
            //            Equip.Name = txtEquipName.Text;
            //            Equip.Price = Convert.ToDecimal(txtEquipPrice.Text);
            //            Equip.Description = txtEquipDesc.Text;
            //            Equip.Category = drpCategory.Text;
            //            Equip.image = imagename;

            //            Equip.save_equipment();

            //            MessageBox.Show("Equipment Added", "Add Equipment", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //        }
            //        else
            //        {

            //        }
            //    }
            //    else
            //    {

            //    }
            //}
            //catch
            //{

            //}

        }
 
    }
}